﻿
package com.example.trackingiodemo;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.reyun.tracking.common.ReYunConst;
import com.reyun.tracking.sdk.Tracking;


public class MainActivity extends Activity
{
	EditText editAppkey;
	EditText editChannelId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ReYunConst.DebugMode = true;
		
		editAppkey = (EditText) this.findViewById(R.id.editText_appKey);
		editChannelId = (EditText) this.findViewById(R.id.editText_channelId);
		//
		// Tracking.initWithKeyAndChannelId(getApplicationContext(), "8283e21a7484c03edee3d61cc12e93ss", "_default_");
	}
	
	public void clickHandler(View v)
	{
		if (v.getId() == R.id.init)
		{
			String appKey = editAppkey.getEditableText().toString().trim();
			String channelId = editChannelId.getEditableText().toString().trim();
			// if(appKey.equals("")) appKey = "8283e21a7484c03edee3d61cc12e93ss";
			if (appKey.equals("")) appKey = "841113bfbf4422c6a53549a2a60e3c7d";
			if (channelId.equals("")) channelId = "_default_";
			
			Tracking.initWithKeyAndChannelId(getApplicationContext(), appKey, channelId);
			
			Toast.makeText(this, "init:" + appKey, Toast.LENGTH_SHORT).show();
		}
		else if (v.getId() == R.id.register)
		{
			String accountId = System.currentTimeMillis() + "";
			// String accountId = Tracking.getDeviceId();
			Tracking.setRegisterWithAccountID(accountId);
			
			Toast.makeText(this, "register: " + accountId, Toast.LENGTH_SHORT).show();
		}
		else if (v.getId() == R.id.login)
		{
			String accountId = Tracking.getDeviceId();
			Tracking.setLoginSuccessBusiness(accountId);
			
			Toast.makeText(this, "login", Toast.LENGTH_SHORT).show();
		}
		else if (v.getId() == R.id.chargeSuccess)
		{
			String transactionId = System.currentTimeMillis() + "";
			String paymentType = "alipay";
			String currencyType = "CNY";
			float currencyAmount = 5.0f;
			Tracking.setPayment(transactionId, paymentType, currencyType, currencyAmount);
			
			Toast.makeText(this, "chargeSuccess", Toast.LENGTH_SHORT).show();
		}
		else if (v.getId() == R.id.customEvent)
		{
			int i = 1 + new Random().nextInt(11);
			String eventName = "event_" + i;
			Tracking.setEvent(eventName);
			
			Toast.makeText(this, "customEvent: " + eventName, Toast.LENGTH_SHORT).show();
		}
		else if (v.getId() == R.id.uploadOrder)
		{
			String transactionId = System.currentTimeMillis() + "";
			
			String currencyType = "CNY";
			float currencyAmount = 9.04f;
			Tracking.setOrder(transactionId, currencyType, currencyAmount);
			
			String accountId = Tracking.getDeviceId();
			Tracking.setRegisterWithAccountID(accountId);
			
			Toast.makeText(this, "uploadOrder: " + transactionId, Toast.LENGTH_SHORT).show();
		}
		else if (v.getId() == R.id.exit)
		{
			Tracking.exitSdk();
			
			Toast.makeText(this, "exit", Toast.LENGTH_SHORT).show();
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
}
